/**
 * @author casazaft
 */
Package("j2j.effect.dnd").Class("Dispatcher", {
    Dispatcher: function(){
        var listeners = {};
        var g = Import("j2j.dom.Geometry");
        var egr = Import("j2j.event.EventMgr");
        var EVENT_NAME = "dragstart";
        var Query = Import("j2j.dom.Query");
        this.registerInterest = function(dom, type){
            if (!listeners[type]) {
                listeners[type] = [];
            }
            listeners[type].push(dom);
        };
        this.dispatchEvent = function(type, data){
            egr.fireEvent(document, EVENT_NAME, type, data);
        };
        var dispatcher = function(e){
            var type = e.datatype;
            var B = e.data;
            if (!type) 
                return;
            var targets = listeners[type];
            for (var i = 0; i < targets.length; i++) {
                var A = targets[i];
                var childQuery = new Query("#" + B.d.id);
                var haschild = childQuery.exec(A).length === 0 ? false : true;
                if (g.contain(A, B) && !haschild) {
                    egr.fireEvent(A, EVENT_NAME, type, B.d);
                    egr.stopEvent(e);
                    return;
                }
            }
            egr.stopEvent(e);
        };
        egr.addEventListener(document, EVENT_NAME, dispatcher, false);
        this.finalize = function(){
            egr.removeEventListener(document, EVENT_NAME, dispatcher, false);
			this.Super.finalize.call(this);
        };
    },
    Extends: j2j.patterns.Singleton
});
